<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>websocket</title>
</head>
<body>

<div id="container">
    <p>你好啊</p>
</div>

<script>
    var url = "ws://localhost:8080/push";
    var ws;

    window.onload = function() {
        ws = new WebSocket(url);

        ws.onopen = function (ev) {
            console.log(ev);
            ws.send("hello a");
        };

        ws.onclose = function (ev) {
            alert(ev)
        };

        ws.onerror = function (ev) {
            alert("error");
            ws.close(3001, "error");
        };

        ws.onmessage = function (ev) {
            var p = document.createElement("p");
            p.innerHTML = ev.data;

            document.getElementById("container").appendChild(p);
        };
    }
</script>
</body>
</html>